package com.lianxi.service.serviceImpl;


import com.lianxi.mapper.LoginMapper;
import com.lianxi.mapper.RolesMapper;
import com.lianxi.pojo.Role;
import com.lianxi.pojo.User;
import com.lianxi.service.LoginService;
import com.lianxi.utils.CommonResult;
import com.lianxi.utils.CurrentUtil;
import com.lianxi.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
@Transactional
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Autowired
    private RolesMapper rolesMapper;


    @Autowired
    private PasswordEncoder passwordEncoder;


    @Override
    public boolean checkUsername(String username) {
        List<User> users = loginMapper.checkUsername(username);
        if(users!=null&&users.size()>0){
            return false;
        }else {
            return true;
        }
    }
    @Override
    public User userLogin(String username, String password) {
        return loginMapper.userLogin(username,password);
    }

    @Override
    public boolean reginUser(User user) {
        user.setRegTime(DateUtil.getTime());
        int result= loginMapper.reginUser(user);
        if (result>0){
            Integer userId = loginMapper.getUserId(user.getUsername());
            int i = loginMapper.addRoleForRegis(userId);
        }else {
            return false;
        }
        return true;
    }

    @Override
    public User getUserByUsername(String username) {
        return loginMapper.getUserByUsername(username);
    }

    @Override
    public Integer getUserId(String username) {
        return loginMapper.getUserId(username);
    }

    @Override
    public int updaPwdByUid(String password) {
        String userName = CurrentUtil.getcurrentUserName();
        Integer userId = loginMapper.getUserId(userName);
        return loginMapper.updaPwdByUid(userId,passwordEncoder.encode(password)) ;
    }

    @Override
    public int updaUserByUid(String nickname, String email) {
        Integer userId = loginMapper.getUserId(CurrentUtil.getcurrentUserName());
        return loginMapper.updaUserByUid(userId,nickname,email);
    }


    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = loginMapper.finUserByName(s);
        if (user==null){
            CommonResult commonResult = CommonResult.failLogin();
            throw new UsernameNotFoundException("用户不存在");
        }else {
            List<Role> roles = rolesMapper.getRolesByUid(user.getId());
            List<SimpleGrantedAuthority> authorities=new ArrayList<>();
            for (Role role : roles) {
                authorities.add(new SimpleGrantedAuthority(role.getName()));
            }
            return new org.springframework.security.core.userdetails.User(
                    user.getUsername(),
                    user.getPassword(),
                    user.isEnabled(),
                    true,
                    true,
                    true,authorities);
        }
    }
}
